<?php
require_once 'auth.php';
requireLogin();

$currentUserRole = $_SESSION['role'];
// 获取库存数量
$conn = dbConnect();
$sql = "SELECT sum(quantity) AS total FROM inventory";
$result = $conn->query($sql);
$totalQuantity = ($result && $result->num_rows > 0) ? $result->fetch_assoc()['total'] : 0;

// 今日入库数量
$sql = "SELECT sum(quantity) AS total FROM stock_records WHERE operation_type ='in' and DATE(operate_at) = CURDATE()";
$result = $conn->query($sql);
$todayInQuantity = ($result && $result->num_rows > 0) ? $result->fetch_assoc()['total'] : 0;

// 今日出库数量
$sql = "SELECT sum(quantity) AS total FROM stock_records WHERE  operation_type ='out' and DATE(operate_at) = CURDATE()";
$result = $conn->query($sql);
$todayOutQuantity = ($result && $result->num_rows > 0) ? $result->fetch_assoc()['total'] : 0;

// 获取用户数量
$sql = "SELECT count(*) AS total FROM users";
$result = $conn->query($sql);
$totalUsers = ($result && $result->num_rows > 0) ? $result->fetch_assoc()['total'] : 0;

?>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>仓库管理系统 - 仪表盘</title>
    <link rel="stylesheet" href="/dist/css/tabler.min.css">
</head>

<body class="layout-fluid">
    <?php include 'navbar.php'; ?>

    <div class="page">
        <div class="page-wrapper">
            <div class="page-header d-print-none">
                <div class="container-xl">
                    <div class="row g-2 align-items-center">
                        <div class="col">
                            <h2 class="page-title">简报</h2>
                        </div>
                    </div>
                </div>
            </div>
            <div class="page-body">
                <div class="container-xl">
                    <div class="row row-deck row-cards">
                        <div class="col-sm-6 col-lg-3">
                            <div class="card">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="subheader">库存数量</div>
                                    </div>
                                    <div class="h1 mb-3"><?php echo isset($totalQuantity) ? $totalQuantity : 0; ?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-3">
                            <div class="card">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="subheader">今日入库</div>
                                    </div>
                                    <div class="h1 mb-3"><?php echo isset($todayInQuantity) ? $todayInQuantity : 0; ?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-3">
                            <div class="card">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="subheader">今日出库</div>
                                    </div>
                                    <div class="h1 mb-3"><?php echo isset($todayOutQuantity) ? $todayOutQuantity : 0; ?></div>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6 col-lg-3">
                            <div class="card">
                                <div class="card-body">
                                    <div class="d-flex align-items-center">
                                        <div class="subheader">当前用户</div>
                                    </div>
                                    <div class="h1 mb-3"><?php echo $totalUsers ?></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="/dist/js/tabler.min.js"></script>
</body>

</html>